﻿using Data;
using Data.Tables;
using PayrollExpress.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace PayrollExpress.Models
{
    public static class WorkspaceHelper
    {

        #region List Headers

        public enum ListHeaders
        {
            #region Payroll
            
            [Description("Cut-Off Start Date")]
            CutOffStartDate,
            [Description("Cut-Off End Date")]
            CutOffEndDate,
            [Description("Processed By")]
            ProcessedBy,
            [Description("Date Processed")]
            DateProcessed,

            #endregion

            #region Payroll Details

            [Description("Hourly Rate")]
            HourlyRate,
            [Description("Daily Rate")]
            DailyRate,
            [Description("Monthly Rate")]
            MonthlyRate,
            [Description("Regular Work Hours")]
            RegularWorkHours,
            [Description("Over Time Hours")]
            OverTimeHours,
            [Description("Night Differential Hours")]
            NightDifferentialHours,
            [Description("Gross Pay")]
            GrossPay,
            [Description("With Holding Tax")]
            WithHoldingTax,
            [Description("SSS Contribution")]
            SSSContribution,
            [Description("PagIbig Fund")]
            PagIbigFund,
            [Description("Phil Health")]
            PhilHealth,
            [Description("Other Deductions")]
            OtherDeductions,
            [Description("Other Deductions Remarks")]
            OtherDeductionsRemarks,
            [Description("Other Additions")]
            OtherAdditions,
            [Description("Other Additions Remarks")]
            OtherAdditionsRemarks,
            [Description("Net Pay")]
            NetPay

            #endregion
        }

        #endregion

        #region Payroll

        public static PayrollListModel GetPayrollListModel(DatabaseContext db, string userName, int? workLocationID)
        {
            workLocationID = workLocationID.HasValue && workLocationID > 0 ? workLocationID : AdministrationHelper.GetUserWorkLocationID(db, userName);

            var model = new PayrollListModel();
            model.SelectedWorkLocationID = workLocationID.GetValueOrDefault();
            model.WorkLocations = AdministrationHelper.WorkLocationDropDownList();
            model.ListItems = GetPayrollList(workLocationID.GetValueOrDefault());
            return model;
        }

        public static List<PayrollQueryModel> GetPayrollList(int workLocationID)
        {
            var list = new List<PayrollQueryModel>();

            using (var db = new DatabaseContext())
            {
                var paramWorkLocationID = new SqlParameter("@WorkLocationID", workLocationID);

                list = db.Database
                    .SqlQuery<PayrollQueryModel>("sp_GetPayrollList @WorkLocationID", paramWorkLocationID)
                    .ToList();
            }

            return list;

        }

        public static PayrollAddEditModel GetPayrollAddEditModel(DatabaseContext db, string userName, int? workLocationID, int? id = null)
        {
            var model = new PayrollAddEditModel();
            model.WorkLocationName = AdministrationHelper.GetWorkLocationName(db, workLocationID.GetValueOrDefault());            

            if (id == null)
            {
                model.PageAction = BaseHelper.PageAction.Add.ToString();
                model.Type = new Payroll();
                model.Type.WorkLocationID = workLocationID.GetValueOrDefault();
                model.Type.CutOffStartDate = DateTime.Today;
                model.Type.CutOffEndDate = DateTime.Today.AddDays(7);
                model.Type.ProcessedBy = userName;
                model.Type.DateProcessed = DateTime.Today;
            }
            else
            {
                model.PageAction = BaseHelper.PageAction.Edit.ToString();
                model.Type = db.Payrolls.Where(w => w.ID == id).SingleOrDefault<Payroll>();
            }

            return model;
        }

        #endregion
        
        #region Payroll Details

        public static PayrollDetailListModel GetPayrollDetailListModel(DatabaseContext db, int payrollID, bool hasDetails)
        {
            var model = new PayrollDetailListModel();

            var  payroll = db.Payrolls.Where(w => w.ID == payrollID).SingleOrDefault<Payroll>();

            model.PayrollID = payrollID;
            model.WorkLocationName = AdministrationHelper.GetWorkLocationName(db, payroll.WorkLocationID);
            model.CutOffStartDate = payroll.CutOffStartDate.ToString("MMMM dd, yyyy");
            model.CutOffEndDate = payroll.CutOffEndDate.ToString("MMMM dd, yyyy");
            model.ListItems = hasDetails ? GetPayrollDetailList(payrollID): GeneratePayrollDetailList(payrollID);
            
            return model;

        }

        private static List<PayrollDetailQueryModel> GetPayrollDetailList(int payrollID)
        {
            var list = new List<PayrollDetailQueryModel>();

            using (var db = new DatabaseContext())
            {
                var paramPayrollID = new SqlParameter("@PayrollID", payrollID);

                list = db.Database
                    .SqlQuery<PayrollDetailQueryModel>("sp_GetPayrollDetailList @PayrollID", paramPayrollID)
                    .ToList();
            }

            return list;
        }

        private static List<PayrollDetailQueryModel> GeneratePayrollDetailList(int payrollID)
        {
            var list = new List<PayrollDetailQueryModel>();

            using (var db = new DatabaseContext())
            {
                var paramPayrollID = new SqlParameter("@PayrollID", payrollID);

                list = db.Database
                    .SqlQuery<PayrollDetailQueryModel>("sp_GeneratePayrollDetailList @PayrollID", paramPayrollID)
                    .ToList();
            }

            return list;
        }
        
        #endregion

    }
}